/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dominio.Persona;
import dominio.Rol;
import fachada.PersonaDAO;
import java.io.Serializable;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author rodrifonsi
 */
@ManagedBean(name = "personaController")
@SessionScoped
public class PersonaController implements Serializable {

    @EJB
    PersonaDAO personaDAO;
    Persona pc;
    int idRol;//Variable para almacenar el valor del rol seleccionado
    DataModel model;

    public PersonaController() {
        pc = new Persona();
    }

    public String create() {
        try {
            pc.setRolId(new Rol(idRol));

            personaDAO.create(pc);
        } catch (Exception e) {
            System.err.print(e.getStackTrace());
        }
        return "listarPersona";
    }

    public String update() {
        try {
            pc.setRolId(new Rol(idRol));

            personaDAO.update(pc);
        } catch (Exception e) {
            System.err.print(e.getStackTrace());
        }
        return "listarPersona";
    }

    public String delete() {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();
            int id = Integer.parseInt(requestParameterMap.get("idPersona").toString());
            this.pc = personaDAO.getPersona(id);
            personaDAO.delete(pc);
        } catch (Exception e) {
            System.err.print(e.getStackTrace());
        }
        return "listarPersona";
    }

    public String editSetup() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();
        int id = Integer.parseInt(requestParameterMap.get("idPersona").toString());
        this.pc = personaDAO.getPersona(id);
        return "editarPersona";
    }
    public String createSetup() {
        this.pc = new Persona();
        return "crearPersona";
    }

    public DataModel getListPersona() {

        model = new ListDataModel(personaDAO.getAllPersona());
        return model;
    }

    public void setPc(Persona pc) {
        this.pc = pc;
    }

    public Persona getPc() {
        return pc;
    }

    public int getIdRol() {
        return idRol;
    }

    public void setIdRol(int idRol) {
        this.idRol = idRol;
    }

    public DataModel getModel() {
        return model;
    }

    public void setModel(DataModel model) {
        this.model = model;
    }
}
